package com.tbit.main.controller;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.pojo.*;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 资产预警开关
 *
 * @author LMY
 * @create 2020-11-16 9:53
 */
@RequestMapping("/MachineAlarmController")
@RestController
public class MachineAlarmController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private MachineAlarmService machineAlarmService;
    @Autowired
    private MachineAlarmLogService machineAlarmLogService;
    @Autowired
    private GeoreGionService georeGionService;

    /**
     * 添加和修改
     */
    @RequestMapping("/insertAndUpdate")
    @OperateLog(module = ModuleEnum.MACHINEALARM, methods = MethodEnum.MACHINEALARM_ADD)
    public JsonResult<Integer> insertAndUpdate(MachineAlarm machineAlarm) {
        Assert.notNull(machineAlarm.getAccountId(), "AccountUser.agentId.notNull");

        MachineAlarm machineAlarmDb = machineAlarmService.getByAccountId(machineAlarm.getAccountId());
        if (machineAlarmDb == null) {
            machineAlarmService.insert(machineAlarm);
        } else {
            machineAlarmService.update(machineAlarm, machineAlarmDb);
        }

        return JsonResult.succ();
    }

    /**
     * 查询
     */
    @RequestMapping("getByAccountId")
    public JsonResult<MachineAlarm> getByAccountId(Integer accountId) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        MachineAlarm machineAlarmDb = machineAlarmService.getByAccountId(accountId);

        if (machineAlarmDb == null) {
            machineAlarmDb = new MachineAlarm();
            machineAlarmDb.loadDefaultField();
            return JsonResult.succ(machineAlarmDb);
        }

        return JsonResult.succ(machineAlarmDb);
    }

    /**
     * 查询记录
     */
    @RequestMapping("getMachinealarmLog")
    public JsonResult<List<MachineAlarmLog>> getMachineAlarmLog(String token, Integer accountId , String startTime, String endTime, Integer alarmType,
                                         @RequestParam(value = "regionId", required = false) List<Integer> regionId) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        AccountUser accountUserToken = tokenService.getAccount(token);

        if (CollUtil.isEmpty(regionId)) {
            regionId = georeGionService.getByAccountUserId(accountUserToken.getAccountUserId());
        }

        List<MachineAlarmLog> machineAlarmLogList = machineAlarmLogService.getMachineLog(accountId, accountUserToken.getAccountUserId(), startTime, endTime, alarmType, regionId);

        return JsonResult.succ(machineAlarmLogList);
    }
}
